package com.lino.design;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description: 活动服务类
 * @author: lingjian
 * @createDate: 2023/2/8 10:50
 */
public class ActivityService {

    private static Map<String, Enum<Status>> statusMap = new ConcurrentHashMap<>();

    /**
     * 初始化活动
     *
     * @param activityId 活动ID
     * @param status     活动状态
     */
    public static void init(String activityId, Enum<Status> status) {
        // 模拟查询活动信息
        ActicityInfo acticityInfo = new ActicityInfo();
        acticityInfo.setActicityId(activityId);
        acticityInfo.setActicityName("早起学习打卡领奖活动");
        acticityInfo.setStatus(status);
        acticityInfo.setBeginTime(new Date());
        acticityInfo.setEndTime(new Date());
        statusMap.put(activityId, status);
    }

    /**
     * 查询活动信息
     *
     * @param activityId 活动ID
     * @return 活动信息
     */
    public static ActicityInfo queryActivityInfo(String activityId) {
        // 模拟查询活动信息
        ActicityInfo acticityInfo = new ActicityInfo();
        acticityInfo.setActicityId(activityId);
        acticityInfo.setActicityName("早起学习打卡领奖活动");
        acticityInfo.setStatus(statusMap.get(activityId));
        acticityInfo.setBeginTime(new Date());
        acticityInfo.setEndTime(new Date());
        return acticityInfo;
    }

    /**
     * 查询活动状态
     *
     * @param activityId 活动ID
     * @return 活动状态
     */
    public static Enum<Status> queryActivityStatus(String activityId) {
        return statusMap.get(activityId);
    }

    /**
     * 执行状态变更
     *
     * @param activityId   活动ID
     * @param beforeStatus 变更前状态
     * @param afterStatus  变更后状态
     */
    public static synchronized void execStatus(String activityId, Enum<Status> beforeStatus, Enum<Status> afterStatus) {
        if (!beforeStatus.equals(statusMap.get(activityId))) {
            return;
        }
        statusMap.put(activityId, afterStatus);
    }
}
